<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { computed, reactive, ref, watch } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { TagDTO } from "@/api/airport/dataManage/tagManage";
import {
  batchSetDeptApi,
  batchSetTagApi
} from "@/api/airport/dataManage/sirSelfCheck";

interface Props {
  type: "tag" | "dept";
  modelValue: boolean;
  ids: number[];
  deptTree?: any[];
  tagOptions?: TagDTO[];
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const title = computed(() => {
  return props.type === "tag" ? "批量设置标签" : "批量设置归属部门";
});

const formData = reactive<{
  tagId?: number;
  belongDept?: number;
}>({
  tagId: undefined,
  belongDept: undefined
});

const rules: FormRules = {
  tagId: [{ required: true, message: "请选择标签", trigger: "change" }],
  belongDept: [{ required: true, message: "请选择归属部门", trigger: "change" }]
};

const formRef = ref<FormInstance>();
const loading = ref(false);

async function handleConfirm() {

  try {
    await formRef.value?.validate();
    loading.value = true;
    debugger
    if (props.type === "tag") {
      await batchSetTagApi(props.ids, formData.tagId);
    } else {
      await batchSetDeptApi(props.ids, formData.belongDept);
    }
    ElMessage.success("操作成功");
    visible.value = false;
    emits("success");
  } catch (e) {
    console.error(e);
  } finally {
    loading.value = false;
  }
}

watch(
  () => props.modelValue,
  val => {
    if (val) {
      // Reset form when modal opens
      formRef.value?.resetFields();
      formData.tagId = undefined;
      formData.belongDept = undefined;
    }
  }
);
</script>

<template>
  <v-dialog
    :title="title"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    class="small-update-modal"
    width="500px"
  >
    <el-form :model="formData" :rules="rules" ref="formRef" label-width="100px" style="margin-top: 60px;margin-left: 30px">
      <el-form-item v-if="type === 'dept'" label="归属部门" prop="belongDept">
        <el-cascader
          v-model="formData.belongDept"
          :options="deptTree"
          :props="{
            value: 'value',
            label: 'label',
            emitPath: false,
            checkStrictly: true
          }"
          clearable
          placeholder="请选择部门"
          class="!w-[200px]"
        />
      </el-form-item>

      <el-form-item v-if="type === 'tag'" label="标签" prop="tagId">
        <el-select
          v-model="formData.tagId"
          placeholder="请选择标签"
          clearable
          filterable
          class="!w-[200px]"
        >
          <el-option
            v-for="item in tagOptions"
            :key="item.tagId"
            :label="item.tagName"
            :value="item.tagId"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </v-dialog>
</template>

<style>
/* You might need a global style block (without 'scoped')
  or a deep selector to pierce the component's scope.
*/
.small-update-modal .el-dialog__body {
  height: 200px !important; /* Or max-height */
  overflow-y: auto !important;
}
</style>

